package com.lewin.ringroad.net.message;

import io.netty.buffer.ByteBuf;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jun
 * @since 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class RealtimeSpeedMessage extends BaseMessage {
    //@formatter:off

    /** POS 线位置 */
    private float posLocation;
    /** 检测通道数量 */
    private short checkChannelNumber;
    /** 通道数据 */
    private List<ChannelData> channelDataList;

    //@formatter:on

    @Override
    public BaseMessage parse(ByteBuf buf) {
        this.posLocation = buf.readFloatLE();
        this.checkChannelNumber = buf.readUnsignedByte();
        this.channelDataList = new ArrayList<>(this.checkChannelNumber);
        for (int i = 0; i < this.checkChannelNumber; i++) {
            this.channelDataList.add(ChannelData.parseFrom(buf));
        }

        return this;
    }

    /**
     * 通道数据
     */
    @Data
    public static class ChannelData {
        /**
         * 检测通道号
         */
        private short code;
        /**
         * 实时速度
         */
        private float speed;

        private static ChannelData parseFrom(ByteBuf buf) {
            ChannelData data = new ChannelData();
            data.code = buf.readUnsignedByte();
            data.speed = buf.readFloatLE();
            return data;
        }
    }
}
